<template>
  <div class="flexContent">
    <!-- 供应商订单 -->
    <div class="searchBox">

      <el-form ref="form" :model="sform" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="采购订单编号">
                  <el-input v-clearEmoji v-model.trim="sform.buyNumber" style="width:95%" clearable
                    placeholder="请输入采购订单编号" @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="收货仓库">
                  <el-select @click.native="reqWarehouseList" v-model="sform.warehouseId" filterable clearable
                    placeholder="请选择收货仓库" popper-class="eloption" style="width:95%">
                    <el-option v-for="item in warehouseLists" :key="item.id" :value="item.id" :label="item.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="供应商">
                  <el-select @click.native="getSCMList" v-model="sform.supplierId" filterable clearable
                    placeholder="请选择供应商" popper-class="eloption" style="width:95%">
                    <el-option v-for="item in scmLists" :key="item.id" :value="item.id" :label="item.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="税率">
                  <el-select @click.native="getOutputRate" v-model="sform.taxRate" placeholder="请选择税率" clearable
                    popper-class="eloption" style="width:95%">
                    <el-option v-for="(val, key) in taxRateLists" :key="key" :value="key" :label="val" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="状态">
                  <el-select v-model="sform.status" placeholder="请选择状态" style="width:95%" clearable>
                    <el-option label="进行中" value="0" />
                    <el-option label="已收货" value="1" />
                    <el-option label="已作废" value="2" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="下单时间">
                  <el-date-picker v-model="date" clearable style="width:95%" type="daterange" unlink-panels
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetForm('form')" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium" align="center">
          <vxe-column type="seq" title="序号" width="80" />
          <vxe-column field="supplierName" title="供应商" :min-width="elTableAdaptationWidth('supplierName', tableData)" />
          <vxe-column field="buyNumber" title="采购订单编号" :min-width="elTableAdaptationWidth('buyNumber', tableData)" />
          <vxe-column field="warehouseName" title="收货仓库"
            :min-width="elTableAdaptationWidth('warehouseName', tableData)" />
          <vxe-column field="totalPrice" title="订单总金额（元）" min-width="150" />
          <vxe-column field="username" title="采购员" min-width="150" />
          <vxe-column field="taxRate" title="税率" min-width="150" />
          <vxe-column title="下单时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.orderTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column field="status" title="状态" width="100">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0'" disabled type="primary" plain size="small">进行中</el-tag>
              <el-tag v-if="row.status === '1'" disabled type="success" plain size="small">已收货</el-tag>
              <el-tag v-if="row.status === '2'" disabled type="danger" plain size="small">已作废</el-tag>
            </template>
          </vxe-column>
          <vxe-column fixed="right" title="操作" width="100" v-if="$showFn(['supplier_order_detail'])">
            <template slot-scope="scope">
              <el-button type="text" @click="detailFn(scope.row)">详情</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getDictValueAndNameSource } from "@/api/dictionary";
import { getSupplierOrderList } from "@/api/supplier";
import { reqWarehouseList } from "@/api/warehouse";
import { getSCMList } from "@/api/scm";
export default {
  name: "SupplierOrder",
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      date: [],
      taxRateLists: [],
      warehouseLists: [],
      scmLists: [],
      isSearch: false,
      sform: {
        buyNumber: "",
        maxDate: "",
        minDate: "",
        supplierId: "",
        taxRate: "",
        warehouseId: "",
        status: "",
      },
      tableData: [],
    };
  },
  created() {
    // this.getOutputRate();
    // this.reqWarehouseList();
    // this.getSCMList();
    this.getSupplierOrderList();
  },
  methods: {
    async reqWarehouseList() {
      const res = await reqWarehouseList();
      if (res.code === 200) {
        this.warehouseLists = res.data;
        console.log(this.warehouseLists, "Ware");
      }
    },
    async getSCMList() {
      const res = await getSCMList();
      if (res.code === 200) {
        this.scmLists = res.data.list;
        console.log(this.scmLists, "Ware");
      }
    },
    async getSupplierOrderList() {
      if (!this.isSearch) {
        this.sform = {
          buyNumber: "",
          maxDate: "",
          minDate: "",
          supplierId: "",
          taxRate: "",
          warehouseId: "",
        };
      }
      const res = await getSupplierOrderList({ ...this.page, ...this.sform });
      // console.log(res)
      if (res.code === 200) {
        //
        this.tableData = res.data.list;
        this.page.pageSize = res.data.pageSize;
        this.page.pageNum = res.data.pageNum;
        this.page.total = res.data.total;
        const $table = this.$refs.xTable1
        $table.loadData(this.tableData);
        console.log(res, "res");
      }
    },
    async getOutputRate() {
      const res = await getDictValueAndNameSource("TAX_RATE");
      if (res.code === 200) {
        this.taxRateLists = res.data;
        // console.log('进项税率', this.inputRateList)
      }
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.sform.minDate = this.date?.length ? this.date[0] : "";
      this.sform.maxDate = this.date?.length ? this.date[1] : "";
      this.getSupplierOrderList();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getSupplierOrderList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getSupplierOrderList();
    },
    resetForm(formName) {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.date = [];
      this.sform = {
        buyNumber: "",
        maxDate: "",
        minDate: "",
        supplierId: "",
        taxRate: "",
        warehouseId: "",
      };
      this.getSupplierOrderList();
    },
    detailFn(row) {
      this.$router.push({
        path: "supplierOrderDetail",
        query: {
          id: row.id,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
